﻿using AutoMapper;
using B.S.DataBase.SuYuan.Domains.Entites;
using B.S.DataBase.SuYuan.ErrorCode;
using B.S.DataBase.SuYuan.Infrastructure.Interfaces;
using B.S.DataBase.SuYuan.Read.API.Applications.Commands.Shop;
using B.S.DataBase.SuYuan.Read.API.Dtos;
using MediatR;
using System.Collections.Generic;

namespace B.S.DataBase.SuYuan.Read.API.Applications.CommandHandlers.Shop
{
    public class ShopFanQueryCommandHandler : IRequestHandler<ShopFanQueryCommand, APIResult<List<ShopInfoDto>>>
    {
        private readonly IBaseRepository<ShopInfoModel> sRepo;
        private readonly IMapper mapper;
        private readonly IBaseRepository<ShopTypeModel> tRepo;
        private readonly IBaseRepository<Imgs> iRepo;

        public ShopFanQueryCommandHandler(IBaseRepository<ShopInfoModel>sRepo,IMapper mapper,IBaseRepository<ShopTypeModel>tRepo,IBaseRepository<Imgs>iRepo)
        {
            this.sRepo = sRepo;
            this.mapper = mapper;
            this.tRepo = tRepo;
            this.iRepo = iRepo;
        }


        public Task<APIResult<List<ShopInfoDto>>> Handle(ShopFanQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<ShopInfoDto>> result = new APIResult<List<ShopInfoDto>>();
            result.Code = ResultCode.查询成功;
            result.Msg = ResultCode.查询成功.ToString();
            var list = from a in sRepo.GetAll()
                       join b in tRepo.GetAll() on a.ShopTypeId equals b.ShopTypeId
                       join c in iRepo.GetAll() on a.BatchId equals c.BatchId
                       where a.ShopTypeId==request.ShopId&&a.BatchId==request.BatchId
                       select new ShopInfoDto
                       {
                           ShopId = a.ShopId,
                           ShopTypeId = a.ShopTypeId,
                           BatchId = a.BatchId,
                           BaaoZhiQi = a.BaaoZhiQi,
                           Productionnumber = a.Productionnumber,
                           Manufacturers = a.Manufacturers,
                           FengMian = a.FengMian,
                           WanShanDu = a.WanShanDu,
                           ImgId = a.ImgId,
                           IsDel = a.IsDel,
                           CreateName = a.CreateName,
                           CreateTime = a.CreateTime,
                           UpdateName = a.UpdateName,
                           UpdateTime = a.UpdateTime,
                           ShopName = b.ShopName,
                           ImgUrl = c.ImgUrl,
                       };
            result.Data = mapper.Map<List<ShopInfoDto>>(list);
            return Task.FromResult(result);



        }



    }
}
